<?php

namespace app\admin\controller;

class Sorting extends Admin{

    public function index(){
        //分类主页
        $Sorting = D('Sorting');
        $list = $Sorting->where('pid=0')->order('sort desc')->select();
        foreach($list as $k => $v){
            $list[$k]['junior'] = $Sorting->where(['pid'=>$v['id']])->order('sort desc')->select();
        }
        return $this->fetch('index',[
            'list' => $list
        ]);
    }

    //添加分类
    public function create(){
        $Sorting = D('Sorting');
        if(IS_POST){
            if(!$_FILES || $_FILES['icon']['size'] == 0){
                return '选择分类图片';
            }
            $post = I('post.');
            //判断是否有重复的分类名称
            $tmp_res = $Sorting->where(['sname'=>$post['sname'],'pid'=>$post['pid']])->find();
            if($tmp_res){
                return '该分类名已经存在';
            }
            if(!preg_match("/^.{2,20}$/",$post['sname'])){
                return '分类名称长度为1-10个字符';
            }

            $Qiniu = A('qiniu','event');
            $fileName = $Qiniu->upload($_FILES['icon']['tmp_name'],'sort_');
            if(!$fileName){
                return '上传失败！';
            }

            $data = [
                'sname' => $post['sname'],
                'image' => $fileName['key'],
                'pid' => I('post.pid/d'),
                'update_time' => I('update_time/d')
            ];
            $res = $Sorting->add($data);
            return $res ? '1' : '添加文件失败';
        }else{
            //查询全部一级分类
            $res = $Sorting->where('pid = 0')->select();
            return $this->fetch('create',['sorts'=>$res]);
        }

    }

    // 修改分类
    public function update(){

        $Sorting = D('Sorting');
        if(IS_POST){
            //判断分类名是否重复
            $map['id'] = ['neq',I('post.sid/d')];
            $map['sname'] = I('post.sname/s');
            $map['pid'] = I('post.pid/d');
            $res = $Sorting->where($map)->find();
            if($res){
                return '类名重复';
            }
            if(!preg_match("/^.{2,20}$/",I('post.sname/s'))){
                return '分类名称长度为1-10个字符';
            }

            $data = [
                'id' => I('post.sid/d'),
                'sname' => I('post.sname/s'),
                'update_time' => I('post.update_time/d')
            ];

            if($_FILES['icon'] && $_FILES['icon']['err'] == 0){
                $Qiniu = A('qiniu','event');
                $Qiniu->delete(I('post.oldImage/s'));
                $img = $Qiniu->upload($_FILES['icon']['tmp_name'],'sort_');
                $data['image'] = $img['key'];
            }

            return $Sorting->where(['id'=>I('post.sid/d')])->setField($data);
        }else{
            $res = $Sorting->where(['id'=>I('get.sid/d')])->find();
            if(!$res){
                return $this->myError('参数错误！');
            }
            //查询全部一级分类
            $list = $Sorting->where('pid = 0')->select();
            return $this->fetch('update',['res'=> $res,'list' => $list ] );
        }
    }

    //更改用户排序
    public function setSort(){
        if(IS_AJAX){
            if(!I('post.sid/d')){
                return '参数错误';
            }
            $Sorting = D('Sorting');

            $data = [
                'sort' => I('post.sort/d'),
                'update_time' => I('post.update_time')
            ];
            return $Sorting->where(['id'=>I('post.sid/d')])->setField($data);
        }
    }

    public function delete(){
        if(IS_AJAX){
            $sid = I('post.sid/d');
            if(!$sid){
                return '参数错误';
            }
            $Sorting = D('Sorting');
            $res = $Sorting->where(['id'=>$sid])->find();

            if(!$res){
                return 1;
            }

            $Qiniu = A('qiniu','event');
            $Qiniu->delete(substr(strrchr($res['image'],'/'),1));

            return $Sorting->where(['id'=>$res['id']])->delete();

        }else{
            return $this->myError('页面不存在');
        }
    }
}
